<template>
  <p-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel">
    <p-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <p-form :form="form">
        <p-row>

          <p-col :span="12">
            <p-form-item label="bom编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'bomCode', validatorRules.bomCode]" placeholder="请输入bom编码"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="bom名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'bomName', validatorRules.bomName]" placeholder="请输入bom名称"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="产品编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'productCode', validatorRules.productCode]" placeholder="请输入产品编号"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="产品名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'productName', validatorRules.productName]" placeholder="请输入产品名称"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="产品规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'productSpecification', validatorRules.productSpecification]" placeholder="请输入产品规格"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="产品型号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'productModel', validatorRules.productModel]" placeholder="请输入产品型号"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="产品描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'comment', validatorRules.comment]" placeholder="请输入产品描述"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="更新人标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'updatetimeBy', validatorRules.updatetimeBy]" placeholder="请输入更新人标识"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="更新时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择更新时间" v-decorator="[ 'updatetimeTime', validatorRules.updatetimeTime]" :trigger-change="true" style="width: 100%"/>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="时间标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择时间标识" v-decorator="[ 'timestamp', validatorRules.timestamp]" :trigger-change="true" style="width: 100%"/>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="公司标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'companyId', validatorRules.companyId]" placeholder="请输入公司标识"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="工厂标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'factoryId', validatorRules.factoryId]" placeholder="请输入工厂标识"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="租户标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'clientId', validatorRules.clientId]" placeholder="请输入租户标识"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="替换人员标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'substituteId', validatorRules.substituteId]" placeholder="请输入替换人员标识"></p-input>
            </p-form-item>
          </p-col>

        </p-row>
      </p-form>

      <!-- 子表单区域 -->
      <p-tabs v-model="activeKey" @change="handleChangeTabs">
        <p-tab-pane tab="bom物料明细表" :key="refKeys[0]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[0]"
            :loading="bomDetailTable.loading"
            :columns="bomDetailTable.columns"
            :dataSource="bomDetailTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="true"/>
        </p-tab-pane>
        
      </p-tabs>

    </p-spin>
  </p-modal>
</template>

<script>

  import pick from 'lodash.pick'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'  

  export default {
    name: 'ProductBomModal',
    mixins: [JEditableTableMixin],
    components: {
      JDate,
    },
    data() {
      return {
        labelCol: {
          span: 6
        },
        wrapperCol: {
          span: 16
        },
        labelCol2: {
          span: 3
        },
        wrapperCol2: {
          span: 20
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
          bomCode: {rules: [
          ]},
          bomName: {rules: [
            {required: true, message: '请输入bom名称!'},
          ]},
          productCode: {rules: [
          ]},
          productName: {rules: [
          ]},
          productSpecification: {rules: [
          ]},
          productModel: {rules: [
          ]},
          comment: {rules: [
          ]},
          updatetimeBy: {rules: [
          ]},
          updatetimeTime: {rules: [
          ]},
          timestamp: {rules: [
          ]},
          companyId: {rules: [
          ]},
          factoryId: {rules: [
          ]},
          clientId: {rules: [
          ]},
          substituteId: {rules: [
          ]},
        },
        refKeys: ['bomDetail', ],
        tableKeys:['bomDetail', ],
        activeKey: 'bomDetail',
        // bom物料明细表
        bomDetailTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '物料编号',
              key: 'code',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '描述',
              key: 'comment',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '更新人标识',
              key: 'updatetimeBy',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '更新时间',
              key: 'updatetimeTime',
              type: FormTypes.date,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '时间标识',
              key: 'timestamp',
              type: FormTypes.date,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '公司标识',
              key: 'companyId',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '工厂标识',
              key: 'factoryId',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '租户标识',
              key: 'clientId',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '替换人员标识',
              key: 'substituteId',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
          ]
        },
        url: {
          add: '/basic-business/basicProductBom/productBom/add',
          edit: '/basic-business/basicProductBom/productBom/edit',
          bomDetail: {
            list: '/basic-business/basicProductBom/productBom/queryBomDetailByMainId'
          },
        }
      }
    },
    methods: {
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        let fieldval = pick(this.model,'bomCode','bomName','productCode','productName','productSpecification','productModel','comment','updatetimeBy','updatetimeTime','timestamp','companyId','factoryId','clientId','substituteId')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.bomDetail.list, params, this.bomDetailTable)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)

        return {
          ...main, // 展开
          bomDetailList: allValues.tablesValue[0].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'bomCode','bomName','productCode','productName','productSpecification','productModel','comment','updatetimeBy','updatetimeTime','timestamp','companyId','factoryId','clientId','substituteId'))
     },

    }
  }
</script>

<style scoped>
</style>